﻿//Tab控制选项卡
function tabs(tabId, event) {
    //绑定事件
    var tabItem = $(tabId + " #tab_head ul li a");
    tabItem.bind(event, function () {
        //设置点击后的切换样式
        tabItem.removeClass("current");
        $(this).addClass("current");
        //设置点击后的切换内容
        var tabNum = tabItem.parent().index($(this).parent());
        $(tabId + " .tab_inner").hide();
        $(tabId + " .tab_inner").eq(tabNum).show();
    });
}



//团购限时显示时间
function startTime(obj, longStartTime, longEndTime) {
    var tmrStart = longStartTime;
    var tmrEnd = longEndTime;
    var _L = 9;
    var tmrHandle = setInterval(function _tmrFunc() {
        tmrStart = tmrStart + 0.1;
        _L--;
        if (tmrStart > tmrEnd) {
            clearInterval(tmrHandle);
            //window.location.reload();
            return;
        }
        if (_L < 0) { _L = 9; }
        //var _D  = parseInt((tmrEnd-tmrStart) % 86400);
        var _H = parseInt((tmrEnd - tmrStart) % 86400 / 3600) + parseInt((tmrEnd - tmrStart) / 86400) * 24;
        var _M = parseInt((tmrEnd - tmrStart) % 3600 / 60);
        var _S = parseInt((tmrEnd - tmrStart) % 60);
        var str = '<span class="icon"></span>' + _H + '<em>时</em>' + _M + '<em>分</em>' + _S + ":" + _L + '<em>秒</em>';
        $(obj).html(str);
    }, 100);
}



//==================图文详细页函数=====================
//载入第一张图片
$(function(){
    $(".jqzoom img").attr("jqimg",$(".items li img").eq(0).attr("bimg"));
    $(".jqzoom img").attr("src",$(".items li img").eq(0).attr("src"));
});

//图片放大镜效果
$(function(){
	$(".jqzoom").jqueryzoom({xzoom:315,yzoom:352});
});

//图片预览小图移动效果,页面加载时触发
$(function(){
	var tempLength = 0; //临时变量,当前移动的长度
	var viewNum = 4; //设置每次显示图片的个数量
	var moveNum = 2; //每次移动的数量
	var moveTime = 300; //移动速度,毫秒
	var scrollDiv = $(".spec-scroll .items ul"); //进行移动动画的容器
	var scrollItems = $(".spec-scroll .items ul li"); //移动容器里的集合
	var moveLength = scrollItems.eq(0).width() * moveNum; //计算每次移动的长度
	var countLength = (scrollItems.length - viewNum) * scrollItems.eq(0).width(); //计算总长度,总个数*单个长度
	  
	//下一张
	$(".spec-scroll .next").bind("click",function(){
		if(tempLength < countLength){
			if((countLength - tempLength) > moveLength){
				scrollDiv.animate({left:"-=" + moveLength + "px"}, moveTime);
				tempLength += moveLength;
			}else{
				scrollDiv.animate({left:"-=" + (countLength - tempLength) + "px"}, moveTime);
				tempLength += (countLength - tempLength);
			}
		}
	});
	//上一张
	$(".spec-scroll .prev").bind("click",function(){
		if(tempLength > 0){
			if(tempLength > moveLength){
				scrollDiv.animate({left: "+=" + moveLength + "px"}, moveTime);
				tempLength -= moveLength;
			}else{
				scrollDiv.animate({left: "+=" + tempLength + "px"}, moveTime);
				tempLength = 0;
			}
		}
	});
});

//鼠标经过预览图片函数
function preview(img){
	$("#preview .jqzoom img").attr("src",$(img).attr("src"));
	$("#preview .jqzoom img").attr("jqimg",$(img).attr("bimg"));
}
//==================图文详细页函数=====================





function getComment(obj) {
    var aid = $.trim($('#hidArticeId').val());
    var gid = $.trim($('#hidSysModelId').val());
    $.post('/Tools/Ajax.ashx', { a: 'shw_c', aid: aid, gid: gid, top: '5', locked: '-1', rnd: Math.random().toString() }, function (data) {
        if (data == "null") {
            $('#' + obj).html('<p style="line-height:35px;">暂无评论，快来抢沙发吧！</p>');
        } else {
            $('#' + obj).html(data);
        }
    });
}



//删除元素
function HintRemove(obj) {
    $("#" + obj).remove();
}


var cartFlag = true;


//http: //localhost:2072/shop/ajax.ashx?a=add_c&gid=2&aid=128&sum=1

//添加进购物车
function AddCart(obj,gid,aid,sum,url) {
    url = $.trim(url);
    if (isNaN(sum)) {
        sum = $.trim($(sum).val());
    }

    if (!cartFlag) {
        var HintHtml = '<div id="cart_info_hint" class="msg_tips cart_info">'
					+ '<div class="ico error"></div>'
					+ '<div class="msg">'
					+ '<strong>商品添加到购物车失败！</strong>'
					+ '<div>商品正在添加中...</div>'
					+ '<a class="close" title="关闭" href="javascript:void(0);" onclick="HintRemove(\'cart_info_hint\');"><span>关闭</span></a>'
					+ '</div>'
					+ '</div>';
        $(obj).after(HintHtml);
    } else {
        cartFlag = false;
    }



    $.ajax({
        type: 'POST',
        dataType: "json",
        url: '/Shop/Ajax.ashx',
        data: { a: 'add_c', gid: gid, aid: aid, sum: sum, rnd: Math.random().toString() },
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        timeout: 20000,
        success: function (data) {
            if (data.success) {
                if (url != '') {
                    location.href = url;
                } else {
                    var HintHtml = '<div id="cart_info_hint" class="msg_tips cart_info">'
						+ '<div class="ico"></div>'
						+ '<div class="msg">'
						+ '<strong>已成功添加到购物车！</strong>'
						+ '<div>购物车共有<b>' + data.count + '</b>件商品</div>'
						+ '<a class="btn btn-success" title="去收银台结算" href="./confirm.aspx">去收银台</a>&nbsp;&nbsp;'
						+ '<a title="查看我的购物车" href="./shoppingcart.aspx">查看购物车</a>'
						+ '<a class="close" title="关闭" href="javascript:;" onclick="HintRemove(\'cart_info_hint\');"><span>关闭</span></a>'
						+ '</div>'
						+ '</div>';
                    $(obj).after(HintHtml);
                }
            } else {
                var HintHtml = '<div id="cart_info_hint" class="msg_tips cart_info">'
					+ '<div class="ico error"></div>'
					+ '<div class="msg">'
					+ '<strong>商品添加到购物车失败！</strong>'
					+ '<div>' + data.error + '</div>'
					+ '<a class="close" title="关闭" href="javascript:void(0);" onclick="HintRemove(\'cart_info_hint\');"><span>关闭</span></a>'
					+ '</div>'
					+ '</div>';
                $(obj).after(HintHtml);
            }
            cartFlag = true;
        },
        error: function (XMLrequest, status, error) {
            alert("状态：" + status + "；错误：" + error);
            cartFlag = true;
        }
    });


	return false;
}

//删除购物车商品(-1全部删除)
function DeleteCart(cartId){
    if (!confirm("您确认要从购物车中移除吗？")) {
		return false;
    }
    if (!cartFlag) {
        alert('正在操作中...');
    } else {
        cartFlag = false;
    }
    $.post('/Shop/Ajax.ashx', { a: 'del_c', id: cartId, rnd: Math.random().toString() }, function (data) {
        if (data.success) {
            location.reload();
        } else {
            alert(data.error);
        }
        cartFlag = true;
    }, 'json');
    return false;
}



//计算购物车金额
function UpdateCart(cartId, quantity) {
    if (!cartFlag) {
        alert('正在操作中...');
    } else {
        cartFlag = false;
    }

    $.post('/Shop/Ajax.ashx', { a: 'upt_c', id: cartId, sum: quantity, rnd: Math.random().toString() }, function (data) {
        if (data.success) {
            location.reload();
        } else {
            alert(data.error);
        }
        cartFlag = true;
    }, 'json');
	return false;
}


function TotalCart(obj, cartId) {
    var num = parseInt($(obj).val());
	if(num > 0){
		UpdateCart(cartId, num);
	}else{
		alert("数量不能是负数！");
	}
}

//购物车数量加减
function ComputCart(obj, cartId, num) {
    if (num > 0) {
        var goods_quantity = $(obj).prev("input[name='goods_quantity']");
        var quantity_value = parseInt($(goods_quantity).val()) + 1;
        $(goods_quantity).val(quantity_value);
        //计算购物车金额
        UpdateCart(cartId, quantity_value);
    } else {
        var goods_quantity = $(obj).next("input[name='goods_quantity']");
        if (parseInt($(goods_quantity).val()) > 1) {
            var quantity_value = parseInt($(goods_quantity).val()) - 1;
            $(goods_quantity).val(parseInt($(goods_quantity).val()) - 1);
            //计算购物车金额
            UpdateCart(cartId, quantity_value);
        }
    }
}



//计算配送费用总金额
function FreightAmountTotal(obj){
	var distribution_price = $(obj).next("input[name='distribution_price']").val();
	$("#freight_amount").text(distribution_price); //运费
	OrderAmountTotal();
}
//计算订单总金额
function OrderAmountTotal(){
	var goods_amount = $("#goods_amount").text(); //商品总金额
	var freight_amount = $("#freight_amount").text(); //运费
	var order_amount = parseFloat(goods_amount) + parseFloat(freight_amount);   //订单总金额 = 商品金额 + 手续费 + 运费
	$("#order_amount").text(order_amount.toFixed(2));
}


////计算支付手续费总金额
//function PaymentAmountTotal(obj) {
//    var payment_price = $(obj).next("input[name='payment_price']").val();
//    $("#payment_fee").text(payment_price); //运费
//    OrderAmountTotal();
//}
////计算订单总金额
//function OrderAmountTotal() {
//    var goods_amount = $("#goods_amount").text(); //商品总金额
//    var payment_fee = $("#payment_fee").text(); //手续费
//    var freight_amount = $("#freight_amount").text(); //运费
//    var order_amount = parseFloat(goods_amount) + parseFloat(payment_fee) + parseFloat(freight_amount);   //订单总金额 = 商品金额 + 手续费 + 运费
//    $("#order_amount").text(order_amount.toFixed(2));
//}
